package com.example.producer;

import com.example.util.ConnectionUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class PublisherUsingFanoutExchange {

    // 定义交换机的名称
    private static final String EXCHANGE_NAME = "test-fanout-exchange";

    public static void main(String[] args) throws IOException, TimeoutException {

        try(Connection connection = ConnectionUtils.openConnection(null);
            Channel channel = connection.createChannel()){

            // 声明exchange，指定类型为fanout
            // fanout表示广播，将消息交给所有绑定到交换机的队列
            channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
            // 消息内容
            String message = "注册成功！！";
            // 发布消息到Exchange
            channel.basicPublish(EXCHANGE_NAME, "", null, message.getBytes());
            System.out.println(" [生产者] Sent '" + message + "'");
        }
    }
}
